package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.FanConfig;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.IFanConfigService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 
 * </p>
 *
 * @author chenxuebin
 * @since 2020-08-10
 */
@Controller
public class FanConfigController {

    private static final Logger logger = LoggerFactory.getLogger(FanConfigController.class);

    @Autowired
    private IFanConfigService fanconfigService;

    @RequestMapping("admin/fanconfig/index")
    public String index() {
        return "admin/fanconfig";
    }

    @RequestMapping("/admin/fanconfig/list")
    @ResponseBody
    public Page<FanConfig> list(@RequestParam(value="limit",required = false) int limit,
                                @RequestParam(value="offset",required = false) int offset) {
        Wrapper<FanConfig> wrapper = new EntityWrapper<FanConfig>().eq("mpid",ObjectUtil.getMpid());
        wrapper.orderBy("id",true);
        Page<FanConfig> page = fanconfigService.selectPage(new Page<FanConfig>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/fanconfig/add")
    public String add(@Valid FanConfig fanconfig) {

        fanconfig.setMpid(ObjectUtil.getMpid());
        fanconfig.setCreatetime(new Date());

        fanconfig.insert();
        return "admin/fanconfig";
    }

    @RequestMapping("/admin/fanconfig/getFanConfigById")
    @ResponseBody
    public FanConfig getFanConfigById(long id) {
        FanConfig fanconfig = fanconfigService.selectById(id);
        return fanconfig;
    }

    @RequestMapping("/admin/fanconfig/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = fanconfigService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/fanconfig/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        FanConfig fanconfig = new FanConfig();
        fanconfig.setId(id);
        fanconfig.setState(state);
        fanconfigService.updateById(fanconfig);
        return "success";
    }

    @RequestMapping("/admin/fanconfig/modify")
    public String update(FanConfig fanconfig) {
        boolean isSuccc = fanconfigService.updateById(fanconfig);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    /// / ************* bellow is fanconfig interface for user *********
//
//    @RequestMapping("/api/fanconfig/list")
//    @ResponseBody
//    public Jinfo list(@RequestParam(value="status",required = false)String status,
//                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
//                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
//                                HttpServletRequest req) {
//        Jinfo info = new Jinfo();
//        if(pageSize==null)pageSize = 10;
//        Page page=new Page(pageIndex,pageSize);
//        page = fanconfigService.selectPage(page, new EntityWrapper<FanConfig>()
//                .eq("uid", ObjectUtil.getUid())
//                .eq("state", 1));
//        info.setInfo(page.getRecords());
//        return info;
//    }
//
//    @RequestMapping("/api/fanconfig/add")
//    @ResponseBody
//    public Jinfo fanconfigAdd(@Valid FanConfig fanconfig,
//                          BindingResult bindingResult,HttpServletRequest req) {
//        Jinfo info = new Jinfo();
//        // 字段校验
//        if(bindingResult.hasErrors())
//        {
//            logger.debug( "There are errors! {}", bindingResult );
//            info.setMsg("There are errors! "+bindingResult);
//            info.setCode(400);
//            return info;
//        }
//        fanconfig.setCreatetime(new Date());
//        fanconfig.insert();
//        return info;
//    }
//
//    @RequestMapping("/api/fanconfig/modify")
//    @ResponseBody
//    public Jinfo fanconfigUpdate(HttpServletRequest req,
//                                    FanConfig fanconfig) {
//        Jinfo info = new Jinfo();
//        fanconfig.setModifytime(new Date());
//        fanconfig.updateById();
//        return info;
//    }
//
//    @RequestMapping("/api/fanconfig/detail")
//    @ResponseBody
//    public Jinfo getFanConfigDetailById(@RequestParam("token") String token,
//                                       @RequestParam("id") long id,HttpServletRequest req
//    ) {
//        Jinfo info = new Jinfo();
//        FanConfig fanconfig = fanconfigService.selectById(id);
//        info.setInfo(fanconfig);
//        return info;
//    }
}
